package com.rdc.jiajiadictionary.notebook;

import android.content.Context;
import android.media.MediaPlayer;
import android.view.View;


import com.rdc.jiajiadictionary.base.BasePresenter;
import com.rdc.jiajiadictionary.bean.Word;
import com.rdc.jiajiadictionary.data.getnotebook.NotebookModel;
import com.rdc.jiajiadictionary.data.getnotebook.NotebookModelInterface;

import java.io.IOException;
import java.util.List;

import static com.rdc.jiajiadictionary.adapter.NotebookAdapter.NO_CHECKBOX;

public class NotebookPresenter implements BasePresenter {

    private MediaPlayer mediaPlayer;
    private List<Word> words;
    private NotebookViewInterface mNotebookViewInterface;
    private NotebookModelInterface mNotebookModelInterface;
    @Override
    public void start() {
        displayNotebookByDefault(NO_CHECKBOX);
    }

    public NotebookPresenter(NotebookViewInterface notebookViewInterface, Context context){
        mNotebookViewInterface = notebookViewInterface;
        mNotebookViewInterface.setPresenter(this);
        mNotebookModelInterface = new NotebookModel(context);
    }


    /**
     * 调用model层方法删除生词本中的所有单词
     */
    public void deleteAllNotebook(){
        mNotebookModelInterface.deleteAllNotebookInDatabase();
    }

    public void deleteWordFromNotebook(String wordName){
        mNotebookModelInterface.deleteWordFromNotebookInDatabase(wordName);
    }
    /**
     * 默认排序生词本单词
     */
    public void displayNotebookByDefault(int visible){
        mNotebookViewInterface.adaptNotebookRecycleView(mNotebookModelInterface.getNotebookWordFromDatabase("desc"),visible);
    }

    /**
     * 字母排序生词本单词
     */
    public void displayNotebookByAlphabet(int visible){
        mNotebookViewInterface.adaptNotebookRecycleView(mNotebookModelInterface.getNotebookWordFromDatabase("asc"),visible);
    }

    public void play(String url){

        if(null == mediaPlayer){
            mediaPlayer = new MediaPlayer();
        }

        if(!mediaPlayer.isPlaying()){
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(url);
                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
